<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="用户名" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="110" align="center">
        <template slot-scope="scope">
          <img class="img" :src="scope.row.avatar" alt="avatar">
        </template>
      </el-table-column>
      <el-table-column label="介绍">
        <template slot-scope="scope">{{ scope.row.introduce }}</template>
      </el-table-column>
      <el-table-column label="权限" width="110" align="center">
        <template slot-scope="scope">{{ scope.row.roles }}</template>
      </el-table-column>
      <el-table-column class-name="status-col" label="技能" width="220" align="center">
        <template slot-scope="scope">
          <el-tag
            v-for="(item,index) in JSON.parse(scope.row.skills)"
            :key="index"
            class="tag"
            :type="item | statusFilter"
          >{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="加入时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ new Date(+scope.row.create_times).toLocaleDateString() }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList } from '@/api/table'

export default {
  filters: {
    statusFilter (status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data () {
    return {
      list: null,
      listLoading: true
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.img {
  width: 40px;
  height: 40px;
}
.tag + .tag {
  margin-left: 5px;
}
</style>
